#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char *argv[]) {
  pid_t pid;
  pid = fork();

  if (pid > 0) {
    char *data;
    data = malloc(10000000);
    int ret;
    if (access("./myfifo", F_OK) != 0) {
      ret = mkfifo("./myfifo", 0666);
      if (ret < 0) {
        perror("mkfifo fail");
        exit(-1);
      }
    }

    ret = open("./myfifo", O_RDWR);
    if (ret < 0) {
      perror("open fail");
      exit(-1);
    }

    ret = read(ret, data, 10000000);
    printf("read is %d\n", ret);

    exit(0);
  } else if (pid == 0) {
    int ret;
    if (access("./myfifo", F_OK) != 0) {

      ret = mkfifo("./myfifo", 0666);
      if (ret < 0) {
        perror("mkfifo fail");
        exit(-1);
      }
    }

    ret = open("./myfifo", O_RDWR);
    if (ret < 0) {
      perror("open fail");
      exit(-1);
    }

    char x = '1';
    int i = 0;
    while (1) {
      write(ret, &x, 1);
      i++;
      printf("write is %d\n",i);
    }    
  }

  return 0;
}

